function B=maproots_mat(A,r)

n=size(A,1);

m=length(r);

B=cell(n);

for k=1:n
    
    for l=1:n
        
        if k==l,
            B{k,k}=1;
            rA{k}=roots(A{k,k});
        else
            B{k,l}=0;
        end
        
    end
    
end

for j=1:m
    
    for k=1:n
        
        if ~isempty(rA{k}),
            
            [mind(k), minind(k)]=min(abs(r(j)-rA{k}));
            
        else
            mind(k)=inf;
            minind(k)=nan;
        end
        
    end
    
    [~, mink]=min(mind);
    [~, rkj]=min(abs(r(j)-rA{mink}));
    
    B{mink,mink}=conv(B{mink,mink},[1 -r(j)]);
    
    aux=rA{mink};
    
    rA{mink}=aux([1:(rkj-1) (rkj+1):length(rA{mink})]);
        
end
    
